package com.compton.UI
{
	import com.compton.VO.WineVO;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	import libs.LeftEmpty;
	import libs.LeftRating;
	import libs.RightEmpty;
	import libs.RightRating;
	import libs.ThumbBase;
	import libs.ThumbMask;
	
	public class Thumbnails extends ThumbBase
	{
		private var _imagesVO:WineVO;
		private var _loader:Loader;
		private var _thumbMask:ThumbMask;
		
		public function Thumbnails()
		{
			super();
			this.addEventListener(MouseEvent.CLICK, onClick);
			this.buttonMode = true;
		}
		
		//When a thumbnail is clicked the program will create a new window and display more information about the product
		private function onClick(evt:MouseEvent):void
		{
			var newWindow:WineDetails = new WineDetails();
			newWindow.details = _imagesVO;
			
			var winInit:NativeWindowInitOptions = new NativeWindowInitOptions();
			winInit.type = NativeWindowType.UTILITY;
			winInit.systemChrome = NativeWindowSystemChrome.NONE;
		
			var w:NativeWindow = new NativeWindow(winInit);
			w.activate();
			w.width = newWindow.width;
			w.height = newWindow.height;
			w.stage.scaleMode = StageScaleMode.NO_SCALE;
			w.stage.align = StageAlign.TOP_LEFT;
			w.stage.addChild(newWindow);
		}
		
		//loads images that are passed in from the favorites panel and adds them to the containers inside the thumbnails
		private function loadImages():void
		{
			_loader = new Loader();
			_loader.load(new URLRequest(_imagesVO.image));
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			//TODO: Handle image load errors.
			
			//thumb_Container.addChild(_loader);
			_loader.scaleX = _loader.scaleY = 1;
			
			tfWine.htmlText = HtmlText.convertHTMLEntities(_imagesVO.name);
			tfWine.selectable = false;
			tfWine.mouseEnabled = false;
			
			tfWinery.htmlText = HtmlText.convertHTMLEntities(_imagesVO.winery);
			tfWinery.selectable = false;
			tfWinery.mouseEnabled = false;
			
			tfType.text = _imagesVO.type;
			tfType.selectable = false;
			tfType.mouseEnabled = false;
			
			tfYear.text = _imagesVO.vintage;
			tfYear.selectable = false;
			tfYear.mouseEnabled = false;
			this.addChild(_loader);
			
			_thumbMask = new ThumbMask();
			addChild(_thumbMask);
			_thumbMask.x = 4;
			_thumbMask.y = 3;
			_loader.mask = _thumbMask;
		
			for(var i:int = 0; i < _imagesVO.ranking * 2 ; i++)
			{	
				if(i%2 > 0)
				{	
					var image2:RightRating = new RightRating();
					image2.x = i*(image.width) + 250; 
					image2.y = image.y;
					this.addChild(image2);
						
				}else{
					var image:LeftRating = new LeftRating();
					image.x = i*(image.width) + 250;
					image.y = image.y + 35;
					this.addChild(image);
				}
			}
		
			for(var p:int = 0; p < 10; p++)
			{ 
				if(p%2 > 0)
				{
					var image3:RightEmpty = new RightEmpty();
					image3.x = p*(image4.width) + 250;
					image3.y = image3.y + 35;
					this.addChildAt(image3,4);
				}else{
					var image4:LeftEmpty = new LeftEmpty();
					image4.x = p*(image4.width) + 250;
					image4.y = image4.y + 35;
					this.addChildAt(image4,4);
				}
			}
		}
		
		private function onLoadComplete(evt:Event):void
		{
			var image:Bitmap = evt.currentTarget.content;
			image.x = -(image.width - _thumbMask.width)/2;
			
			if(image.height > image.width*2)
			{
				image.scaleX = 1.4;	
				image.scaleY = 1;
			}
			
		}

		/**
		 * This getter and setter will provide the information about the thumbnail that is about to be created. 
		 * @return returns and recieves the imagesVO to place all neccessary information into each thumbnail
		 * 
		 */		
		public function get imagesVO():WineVO
		{
			return _imagesVO;
		}
		
		/**
		 * @private
		 */
		public function set imagesVO(value:WineVO):void
		{
			_imagesVO = value;
			loadImages();
		}

	}
}